const fs = require('fs');
const path = require('path');
let json = {}
let repeat = []
function readFilesRecursively(folderPath) {
  const files = fs.readdirSync(folderPath);
  files.forEach(file => {
    const filePath = path.join(folderPath, file);
    const stats = fs.statSync(filePath);
    if (stats.isDirectory()) {
      readFilesRecursively(filePath);
    } else if (stats.isFile()) {
      const list = filePath.split('.')
      const suffix = list[list.length - 1]
      if (suffix === 'vue') {
        // 在这里处理文件
        setFile(filePath)
      } else if (suffix === 'js') {
        setFile(filePath)
      }
    }
  });
}

function getText (initContent) {
  const lines = initContent.split(/\r?\n/);
  lines.forEach((line, index) => {
    if (line.includes('//')) return
    if (line.includes('console.')) return
    if (line.includes('/*')) return
    if (line.includes('* ')) return
    if (line.includes('<!--')) return
    // ([“,，。、)(：”]?)
    line.replace(/([\u4E00-\u9FFF](\s?[\u4E00-\u9FFF]?\d?\w?[“,，。、)(：.”]?)+)/g, (v) => {
      v = v.trim()
      let key = v.replace(/\s/g, '')
      key = key.replace(/[“,，。、)(：.”]/g, '')
      if (!json[`${key}`]) {
        json[`${key}`] = `${v}`
      } else {
        if (json[`${key}`] !== `${v}`) {
          const repeatIndex = repeat.findIndex(repeatV => repeatV === `${v}`)
          if (repeatIndex === -1) {
            repeat.push(v)
          }
        }
      }
    })
  });
}

function getTemplateText (initContent) {
  const lines = initContent.split(/\r?\n/);
  lines.forEach((line, index) => {
    if (line.includes('//')) return
    if (line.includes('console.')) return
    if (line.includes('/*')) return
    if (line.includes('* ')) return
    if (line.includes('<!--')) return
    const test = /([\u4E00-\u9FFF]+)/g
    if (test.test(line + '')) {
      line = line.replace(/="(\s+)?((\d?\w?\s?[\u4E00-\u9FFF]?[“,，。、)(：.\-”]?)+)"/g, (content, a, v) => {
        if (test.test(content)) {
          v = v.trim()
          let key = v.replace(/\s/g, '')
          key = key.replace(/[“,，。、)(：.”]/g, '')
          if (!json[`${key}`]) {
            json[`${key}`] = `${v}`
          } else {
            if (json[`${key}`] !== `${v}`) {
              const repeatIndex = repeat.findIndex(repeatV => repeatV === `${v}`)
              if (repeatIndex === -1) {
                repeat.push(v)
              }
            }
          }
          return ''
        }
        return content
      })
      line.replace(/([\u4E00-\u9FFF](\s?[\u4E00-\u9FFF]?\d?\w?[“,，。、)(：.”]?)+)/g, (v) => {
      v = v.trim()
      let key = v.replace(/\s/g, '')
      key = key.replace(/[“,，。、)(：.”]/g, '')
      if (!json[`${key}`]) {
        json[`${key}`] = `${v}`
      } else {
        if (json[`${key}`] !== `${v}`) {
          const repeatIndex = repeat.findIndex(repeatV => repeatV === `${v}`)
          if (repeatIndex === -1) {
            repeat.push(v)
          }
        }
      }
    })
    }
  });
}

function setData(initContent) {
  notFound = []
  let text = ''
  const lines = initContent.split(/\r?\n/);
  let templateNum = 0
  let templateEndNum = 0
  lines.forEach((line) => {
    if (line.includes(`<template`)) {
      templateNum++
    }
    if (line.includes(`</template`)) {
      templateEndNum++
      if (templateNum === templateEndNum) {
        line = line.replace(`</template`, `</1111template`)
      }
    }
    text += '\n' + line
  });
  return text
}

function setFile (path) {
  fs.readFile(path, 'utf8', (err, data) => {
    if (err) {
      console.error(err);
      return;
    }
    data = setData(data)
    data = data.replace(new RegExp('<template[^>]*>([\\s\\S]*?)<\/1111template>', 'g'), (match, initContent) => {
      getTemplateText(initContent)
      return ''
    });
    getText(data)
    let repeatJson = ''
    repeat.forEach(v => {
      let key = v.replace(/\s/g, '')
      key = key.replace(/[“,，。、)(：.”]/g, '')
      repeatJson+= '\n' + `${key}:"${v}",`
      // repeatJson[`${key}`] = `${v}`
    })
    setWriteFile(`./text.js`,  `
      <template>
        <div></div>
      </template>

      <script>
      export default {
        mounted() {
          const json = ${JSON.stringify(json)}
          const repeat = {${repeatJson}}
        }
      }
      </script>
    `)
  });
}

function setWriteFile (path, data) {
  fs.writeFile(path, data, 'utf8', (err) => {
    if (err) {
      console.error(err);
      return;
    }
    // console.log('执行成功')
  });
}

const folderPath = 'D:\\gitee项目\\微前端调度台\\daily-check\\src';
readFilesRecursively(folderPath);

